<?php
/* 
 * 点歌操作类
 * 程序编写：pennyowen/pepsi.zh
 * 联系邮箱：penyowen#gmail.com/pepsi.zh#gmail.com
 * 修改时间：2008-9-30
 */
if (!defined('IN_SITE')){ die('Hacking attempt!'); }
interface FriendlinkDbInter{
	### 功能：提交一个链接记录  参数：$post 提交的内容 #####
    function submitRecord($post);
    ### 功能：删除链接记录  参数：$id 记录id，可以是数组，也可以是当id #####
    function deleteRecord($id);
    ### 功能：得到一条链接记录  参数：$id 记录id #####
    function getOneRecord($id);
    ### 功能：返回链接类型  #####
    function getTypeList($type=false);
    ### 功能：返回链接名称  参数：$id 记录id #####
    function getTypeName($id);
	### 功能：返回某个类型链接 参数：$id 记录类型id #####
	function getOneTypeRecord($id);
	### 功能：返回所有链接名称 #####
    function getAllRecord();
}
class friendlink implements FriendlinkDbInter
{
	public $error = array();
	public $action = '提交';
	function submitRecord($post){
		global $dbconn,$FriendLinkCatalog,$dbtable;
		$post = acrossTrim($post);
		$flag = true;
		if (empty($post[Lname])){
			$flag = false;
			$this->error[e_Lname] = '链接名称不能为空';
		}
		if (empty($post[Lurl])){
			$flag = false;
			$this->error[e_Lurl] = '链接地址不能为空';
		}
		if (!array_key_exists($post[Ltopicid],$FriendLinkCatalog)){
			$flag = false;
			$this->error[e_Ltopicid] = '链接类别选择不正确';
		}
		if ($post[unsetValue]){
			foreach ($post[unsetValue] as $value){
				$post[$value] = '';
			}
		}
		//print_r($this->error);
		unset($post[unsetValue]);
		if ($flag){
			if(is_uploaded_file($_FILES['imagefile']['tmp_name'])){
				srand(time(0)); 
				$random =rand(1,100); 
				$upload_file_name = date("YmdHis",time()).$random;
				$filetype = getPicType($_FILES['imagefile']['type']);
				$save_path = UPLOAD_ROOT.$upload_file_name.$filetype;
				if(move_uploaded_file($_FILES['imagefile']['tmp_name'],$save_path)){
					$post[Lphoto] = $upload_file_name.$filetype;
				}
			}
			if ($post[Llinkid]){
				$this->action = '修改';
				return $dbconn->Query_update($post,$dbtable->frdlink,array('Llinkid'=>$post[Llinkid]));
			}else {
				$this->action = '添加';
				return $dbconn->Query_adate($post,$dbtable->frdlink);
			}
		}
		return false;
	}
	function deleteRecord($id){
		global $dbtable;
		if (empty($id)){ return false ;}
		if (is_array($id)){
			$where = "where Llinkid in(".implode(',',$id).")";
		}else {
			$where = "where Llinkid=$id";
		}
		return deleteRecord($dbtable->frdlink,$where);
	}
	function getOneRecord($id){
		global $dbconn,$dbtable;
		if (empty($id)){ return false ;}
		$query = "select * from ".$dbtable->frdlink." where Llinkid=$id limit 1";
		$record = $dbconn->Getonerecord($query);
		if ($record[Lphoto] && file_exists(UPLOAD_ROOT.$record[Lphoto])){
			$record[Lphoto_show] = UPLOAD_URL.$record[Lphoto];
		}else {
			$record[Lphoto] = '';
		}
		return $record;
	}
	function getTypeList($type=false){
		global $FriendLinkCatalog;
		if($type){
			foreach($FriendLinkCatalog as $key=>$value){
				$row[typename] = $value;
				$row[typeid] = $key;
				$typearray[] = $row;
			}
			return $typearray;
		}else{
			return $FriendLinkCatalog;
		}
	}
	function getTypeName($id){
		global $FriendLinkCatalog;
		return $FriendLinkCatalog[$id];
	}
	function getOneTypeRecord($id){
		global $dbconn,$dbtable;
		if (empty($id)){ return false ;}
		$query = "select * from ".$dbtable->frdlink." where Ltopicid=$id order by Lorder desc,Llinkid desc";
		$record = $dbconn->Fetcharray($query);
		return $record;
	}
	function getAllRecord(){
		$linkType = $this->getTypeList(true);
		$num = count($linkType);
		for($i=0;$i<$num;$i++){
			$links = $this->getOneTypeRecord($linkType[$i][typeid]);
			$linkType[$i][n] = $links;
		}
		return $linkType;
	}

}
?>